<?php

require_once(t3lib_extMgm::extPath('zlib') . 'class.tx_zlib_model.php');

class tx_zlib_collection implements Iterator, Countable
{
	/**
	 * @var integer
	 */
    protected $_count;

    /**
     * @var array
     */
    protected $_data;

    /**
     * @var string
     */
    protected $_modelClass = 'tx_zlib_model';

    /**
     * @var tx_zlib_gateway
     */
    protected $_gateway;

    /**
     * Constructor
     *
     * @param array|object $data
     */
    public function __construct($data)
    {
		if(is_object($data)) {
			$data = (array)$data;
		}
		if(!is_array($data)) {
			throw new Exception('tx_zlib_collection::__construct(): Initial data must be an array or object');
		}
        $this->_data = $data;
        $this->_count = count($this->_data);
        $this->init();
    }

    /**
     * @param string $modelClass
     */
    public function setModelClass($modelClass)
    {
    	$this->_modelClass = $modelClass;
    }

    /**
     * @param array $item
     */
    public function add(array $item)
    {
		$this->_data[] = $item;
        $this->_count = count($this->_data);
    }

    /**
     * Initialize object
     *
     * Called from {@link __construct()} as final step of object instantiation.
     *
     * @return void
     */
    public function init()
    {
    }

    /**
     * Set gateway
     *
     * @param tx_zlib_gateway $gateway
     * @return void
     */
    public function setGateway(tx_zlib_gateway $gateway)
    {
        $this->_gateway = $gateway;
    }

    /**
     * Get gateway
     *
     * @return tx_zlib_gateway
     */
    public function getGateway()
    {
        return $this->_gateway;
    }

    /**
     * @return void
     */
    public function reverse()
    {
    	$this->_data = array_reverse($this->_data);
    }

    /**
     * @return integer
     */
    public function count()
    {
        return $this->_count;
    }

    /**
     * @return tx_zlib_model
     */
    public function current()
    {
    	$current = current($this->_data);
    	return new $this->_modelClass($current);
    }

    /**
     * @return integer
     */
    public function key()
    {
        return key($this->_data);
    }

    /**
     * @return array
     */
    public function next()
    {
        return next($this->_data);
    }

    /**
     * @return array
     */
    public function rewind()
    {
        return reset($this->_data);
    }

    /**
     * @return boolean
     */
    public function valid()
    {
    	return array_key_exists($this->key(), $this->_data);
    }

	/**
	 * @return array
	 */
	public function toArray()
	{
		return $this->_data;
	}

	/**
	 * @todo
	 * @return string
	 */
	public function toJson()
	{
	    return json_encode($this->_data);
	}

	/**
	 * @todo
	 * @return string
	 */
	public function toXml()
	{
	}

}


if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zlib/class.tx_zlib_collection.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zlib/class.tx_zlib_collection.php']);
}
